package com.framework.simplemvc.config;

import cn.dev33.satoken.stp.StpUtil;
import com.framework.common.auto.ThreadLocalUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * feign调用服务时，会丢失请求头信息。需要在这里把认证信息收到添加上去
 *
 * @author lihanbo
 * @version 1.0
 * @date 2022/3/31 16:24
 */
@Configuration
@Slf4j
public class FeignTokenInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        if (StpUtil.isLogin()) {
            log.info("====== Feign 添加头部satoken ======");
            String token = StpUtil.getTokenValue();
            requestTemplate.header("satoken", token);
        }
    }
}
